package com.hourse.platform.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @使用描述: StringTools
 */
public class StringTools {

  public StringTools() {
  }

  public static boolean isNotNull(String str) {
    return str != null && !"".equals(str.trim());
  }

  public static boolean isNull(String str) {
    return !isNotNull(str);
  }

  public static String byteToStr(byte[] byteArray) {
    String strDigest = "";

    for(int i = 0; i < byteArray.length; ++i) {
      strDigest = strDigest + byteToHexStr(byteArray[i]);
    }

    return strDigest;
  }

  public static String byteToHexStr(byte mByte) {
    char[] Digit = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    char[] tempArr = new char[]{Digit[mByte >>> 4 & 15], Digit[mByte & 15]};
    String s = new String(tempArr);
    return s;
  }

  public static String byteToHex(byte[] hash) {
    Formatter formatter = new Formatter();
    byte[] var2 = hash;
    int var3 = hash.length;

    for(int var4 = 0; var4 < var3; ++var4) {
      byte b = var2[var4];
      formatter.format("%02x", b);
    }

    String result = formatter.toString();
    formatter.close();
    return result;
  }

  public static String encoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    str = str.trim();
    StringBuffer buf = new StringBuffer("");

    try {
      MessageDigest md = MessageDigest.getInstance("MD5");

      try {
        md.update(str.getBytes("utf-8"));
      } catch (UnsupportedEncodingException var6) {
        var6.printStackTrace();
      }

      byte[] b = md.digest();

      for(int offset = 0; offset < b.length; ++offset) {
        int i = b[offset];
        if (i < 0) {
          i += 256;
        }

        if (i < 16) {
          buf.append("0");
        }

        buf.append(Integer.toHexString(i));
      }
    } catch (NoSuchAlgorithmException var7) {
      var7.printStackTrace();
    }

    return buf.toString();
  }

  public static String defaultSign(Map<String, String> keyValue, String key) throws UnsupportedEncodingException, NoSuchAlgorithmException {
    List<String> params = new ArrayList();
    Iterator var3 = keyValue.entrySet().iterator();

    while(var3.hasNext()) {
      Map.Entry<String, String> entry = (Map.Entry)var3.next();
      params.add(entry.getKey());
    }

    Object[] arr = params.toArray();
    Arrays.sort(arr);
    StringBuffer stringA = new StringBuffer();
    Object[] var5 = arr;
    int var6 = arr.length;

    for(int var7 = 0; var7 < var6; ++var7) {
      Object obj = var5[var7];
      stringA.append(obj);
      stringA.append("=");
      stringA.append((String)keyValue.get(obj));
      stringA.append("&");
    }

    stringA.append("key=");
    stringA.append(key);
    return encoderByMd5(stringA.toString()).toUpperCase();
  }

  public static void signCheck(Map<String, String> keyValue, String timestamp, String sign, String key, Integer maxTime) throws Exception {
    try {
      if (sign.equals(defaultSign(keyValue, key))) {
        Long currentTime = System.currentTimeMillis() / 1000L;
        if (Math.abs(currentTime - Long.valueOf(timestamp)) > (long)maxTime) {
          throw new Exception("请求超时...");
        }
      } else {
        throw new Exception("签名认证失败...");
      }
    } catch (Exception e) {
      throw new Exception(e);
    }
  }

  public static String underline2Camel(String line, boolean smallCamel) {
    if (isNull(line)) {
      return "";
    } else {
      StringBuffer str = new StringBuffer();
      Pattern pattern = Pattern.compile("([A-Za-z\\d]+)(_)?");
      Matcher matcher = pattern.matcher(line);

      while(matcher.find()) {
        String word = matcher.group();
        str.append(smallCamel && matcher.start() == 0 ? Character.toLowerCase(word.charAt(0)) : Character.toUpperCase(word.charAt(0)));
        int index = word.lastIndexOf(95);
        if (index > 0) {
          str.append(word.substring(1, index).toLowerCase());
        } else {
          str.append(word.substring(1).toLowerCase());
        }
      }

      return str.toString();
    }
  }

  public static String camel2Underline(String line) {
    if (isNull(line)) {
      return "";
    } else {
      line = String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
      StringBuffer str = new StringBuffer();
      Pattern pattern = Pattern.compile("[A-Z]([a-z\\d]+)?");
      Matcher matcher = pattern.matcher(line);

      while(matcher.find()) {
        String word = matcher.group();
        str.append(word.toUpperCase());
        str.append(matcher.end() == line.length() ? "" : "_");
      }

      return str.toString();
    }
  }

  public static boolean urlAanalyze(String pattern, String url) {
    pattern = "^(" + pattern;
    pattern = pattern.replaceAll("/\\*\\*", "(/[^\\\\?]{0,}|/{0,1})");
    pattern = pattern.replaceAll("/\\*", "(/[^\\\\?/]{0,})");
    pattern = pattern.replaceAll("\\*\\*", "([^\\\\?]{0,})");
    pattern = pattern.replaceAll("\\*", "([^\\\\?/]{0,}|/{0,1})");
    pattern = pattern.replaceAll("\\.", "\\\\.");
    pattern = pattern + ")$";
    return Pattern.compile(pattern).matcher(url).matches();
  }
}
